
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>

#include "progress.h"

jmp_buf g_eve;

static void ctrl_c(int sig);

/*
 *@brief  初始化
 *@param  progress_bar:进度条
 *@return 参数错误返回EOF，正确返回0
 * */
int init(struct progress_bar *progress_bar)
{	
    if (!progress_bar) {
		printf("param error\n");

		return EOF;
	}

    progress_bar->position_x = 5,
	progress_bar->position_y = 5,
	progress_bar->length = 50,
	progress_bar->wide = 3,
	progress_bar->outline_color = BACK_BLUE,
	progress_bar->inside_color = BACK_GREEN,

    signal(SIGINT, ctrl_c);
    system("clear");
    FLIKER_CURSOR();
    SET_POSITION(5, 2);
    printf(YELLOW"按 enter键加载进度条");
    NONE_CURSOR();

    return 0;
}

/*
 *@brief  运行
 *@param  progress_bar:进度条
 *@return 参数错误返回EOF，正确返回0
 * */
int run(struct progress_bar *progress_bar)
{
    int count;

    if (!progress_bar) {
		printf("param error\n");

		return EOF;
	}

	show_outline(progress_bar);
	
	for (count = 0; count <= 100; count++) {
		show_progress(progress_bar, count);

        if (count < 100)
		    getchar();
        else
            break;
	}
	
	SET_POSITION(5, 2);
    printf(CLEAN_LINE YELLOW"加载结束");
    NONE_CURSOR();
    SET_POSITION(0, progress_bar->position_y + progress_bar->wide);

    return 0;
}

/*
 *@brief  打印进度条外框
 *@param  progress_bar:进度条
 *@return 参数错误返回EOF，正确返回0
 * */
int show_outline(struct progress_bar *progress_bar)
{
	int count_x, count_y;
	
	if (!progress_bar) {
		printf("param error\n");

		return EOF;
	}

	printf("%s", progress_bar->outline_color);

	for (count_y = 0; count_y < progress_bar->wide; count_y++) {

		SET_POSITION(progress_bar->position_x, progress_bar->position_y + count_y);

		/*打印上下边解*/
		if ((count_y == 0) || (count_y == progress_bar->wide - 1)) {
			for (count_x = 0; count_x < progress_bar->length; count_x++) {
				printf(" ");
			}
		}
		/*打印左右边解*/
		else {	
			printf("  ");
			SET_POSITION(progress_bar->position_x + progress_bar->length - 2, progress_bar->position_y + count_y);
			printf("  ");
		}		
	}

	fflush(stdout);
	printf(NONE);

	return 0;
}


/*
 *@brief  打印进度条进度
 *@param  progress_bar:进度条
 *		  percent:进度条加载进度(范围0~100)，如percent=30，代表进度条已加载%30
 *@return 参数错误返回EOF，正确返回0
 * */
int show_progress(struct progress_bar *progress_bar, int percent)
{
	int count_x, count_y;
	
	if (!progress_bar) {
		printf("param error\n");

		return EOF;
	}
	
	printf("%s", progress_bar->inside_color);

	/*打印进度条进度*/
	for (count_y = 1; count_y < progress_bar->wide - 1; count_y++) {

		SET_POSITION(progress_bar->position_x + 2, progress_bar->position_y + count_y);
		
		for (count_x = 0; count_x < percent*(progress_bar->length-4)/100; count_x++) {
			printf(" ");
		}
	}

	SET_POSITION(progress_bar->position_x + progress_bar->length + 1, progress_bar->position_y + 1);
	printf(NONE YELLOW);

	/*显示进度条百分比*/
	printf("%s%d", "%",percent);
	printf(NONE);
	fflush(stdout);

	SET_POSITION(0, progress_bar->position_y + progress_bar->wide);

	return 0;
}


/*Ctrl+C触发信号处理函数*/
static void ctrl_c(int sig)
{
	printf("\n\tpress Ctrl+C will exit\n");
    
	siglongjmp(g_eve, 10);
}

/*释放内存，暂无需释放的内存，预留用*/
void destroy(void)
{
    NONE_CURSOR();
    system("clear");
}